﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WeakScript
{
    public partial class CodeTextBox : RichTextBox
    {
        public ContextListBox _listMenu;
        const string borders = "\n\t\r ;(){}\"=+-*&\"'";

        bool _autoTipIsOn = false;

        public CodeTextBox()
        {
            InitializeComponent();

            _listMenu = new ContextListBox();

            if (_autoTipIsOn)
            {
                _listMenu.Items.Add("abc");
                _listMenu.Items.Add("def");
                _listMenu.Items.Add("gkl");
                _listMenu.Height = _listMenu.Items.Count * _listMenu.ItemHeight;
                Controls.Add(_listMenu);

                _listMenu.Show();
            }

            AcceptsTab = true;
        }

        Char _killChar = '\0';
     

        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if (_autoTipIsOn)
            {
                if (borders.Contains(e.KeyChar)) _listMenu.Hide();
            }
            base.OnKeyPress(e);
        }

        public void ColorRange(int start, int end, Color color)
        {
            int str = this.SelectionStart;

            Select(start, end);
            SelectionColor = color;

            SelectionStart = str;
            SelectionLength = 0;
        }

        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (_autoTipIsOn)
            {
                _listMenu.Height = (_listMenu.Items.Count+1) * _listMenu.ItemHeight;

                if (_listMenu.Items.Count > 0)
                {
                    _listMenu.Show();

                    if (_listMenu.SelectedIndex < 0) _listMenu.SelectedIndex = 0;

                    if (e.KeyCode == Keys.Down)
                    {
                        if (_listMenu.SelectedIndex + 1 < _listMenu.Items.Count) _listMenu.SelectedIndex++;
                        return;
                    }
                    else if (e.KeyCode == Keys.Up)
                    {
                        if (_listMenu.SelectedIndex > 0) _listMenu.SelectedIndex--;
                        return;
                    }
                    else if (e.KeyCode == Keys.Tab)
                    {
                        int pos = SelectionStart;
                        string ins = _listMenu.SelectedItem.ToString();
                        int from = Text.LastIndexOfAny(borders.ToArray()) + 1;
                        if (from < 0) from = 0;

                        Text = Text.Remove(from, pos - from);
                        Text = Text.Insert(from, ins);
                        SelectionStart = from + ins.Length;
                        _killChar = '\t';

                        _listMenu.Items.Clear();
                        _listMenu.Hide();
                        return;
                    }
                }
                else
                {
                    _listMenu.Hide();
                    return;
                }
            }

            base.OnKeyDown(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if (_autoTipIsOn)
            {
                var pos = GetPositionFromCharIndex(SelectionStart);
                _listMenu.Location = pos;
            }       
            base.OnTextChanged(e);     
        }
    }
}
